package com.kbt.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.kbt.activity.R;
import com.kbt.listener.ClickStartActivityListener;
import com.kbt.model.data.GoodForJFInfo;
import com.kbt.model.holder.GoodForJFInfoHolder;
import com.kbt.util.data.StaticData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhgk
 * @package: com.kbt.adapter
 * @TODO: 用于加载要结算的商品信息
 * @data: 2015/8/12 14:09
 * @version: V1.0
 */
public class GoodForJfdhAdapter extends BaseAdapter {

    //商品数据列表
    private List<GoodForJFInfo> goodInfoList;
    //生命上下文
    private Activity context;

    public GoodForJfdhAdapter(Activity context, List<GoodForJFInfo> goodInfoList) {
        this.goodInfoList = goodInfoList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return goodInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return goodInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        GoodForJFInfoHolder itemHolder;

        if (convertView == null) {
            itemHolder = new GoodForJFInfoHolder();

            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.good_jf_duihuan_item, null);

            itemHolder.setGoodImg((ImageView) convertView.findViewById(R.id.jf_good_img));
            itemHolder.setGoodTitle((TextView) convertView.findViewById(R.id.jf_good_title));
            itemHolder.setGoodPrice((TextView) convertView.findViewById(R.id.jf_good_price));
            itemHolder.setGoodCount((TextView) convertView.findViewById(R.id.jf_good_count));
            itemHolder.setGoodTotal((TextView) convertView.findViewById(R.id.jf_good_total));

            convertView.setTag(itemHolder);
        } else {
            itemHolder = (GoodForJFInfoHolder) convertView.getTag();
        }

        GoodForJFInfo goodInfo = goodInfoList.get(position);

        itemHolder.getGoodImg().setImageResource(R.drawable.ic_lancher);
        itemHolder.getGoodTitle().setText(goodInfo.getGoodTitle());
        itemHolder.getGoodPrice().setText("￥" + goodInfo.getGoodPrice());
        itemHolder.getGoodCount().setText("x " + goodInfo.getGoodCount());
        itemHolder.getGoodTotal().setText("总计：￥" + goodInfo.getGoodTotalFee());

        //商品明细打开Activity
        Map params = new HashMap<String, Object>();
        params.put("goodId", goodInfo.getGoodID());
        ClickStartActivityListener startListener = new ClickStartActivityListener(context, StaticData.GOOD_INFO_CLASS, params);
        itemHolder.getGoodImg().setOnClickListener(startListener);

        return convertView;
    }
}
